<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport"
        content="width=device-width,initial-scale=1.0,viewport-fit=cover,maximum-scale=1.0,minimum-scale=1.0,user-scalable=no">
    <title>领取学生证</title>
    <script src="js/rem.js"></script>
    <link rel="stylesheet" href="https://g.alicdn.com/de/prismplayer/2.8.2/skins/default/aliplayer-min.css" />
    <link rel="stylesheet" href="css/weui.min.css">
    <link rel="stylesheet" href="css/jquery-weui.min.css">
    <link rel="stylesheet" href="css/public.css">
    <link rel="stylesheet" href="css/ot-classroom-play.css">
    <link rel="stylesheet" href="css/user.css">
    <script>(function () { var _53code = document.createElement("script"); _53code.src = "https://tb.53kf.com/code/code/96d8f960b261e49515450f8953b044bc5/1"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(_53code, s); })();</script>
    <style>
        .buyCard .tabs_buy_item {
            width: 48%;
            flex: none;
        }
    </style>
</head>

<body>
    <div class="pub_bar" title="安全支付" left="back"></div>
    <!--  -->
    <div class="pub-container has-tabbar bgwhite user-consultation">
        <div class="buyCard">
            <div class="space-border"></div>
            <div class="pub_small_header plr15">
                <a href="#">
                    <div class="title">热门课程 <span>畅学卡权益说明</span></div>
                </a>
            </div>
            <div class="tabs_buy space-border">
            </div>
            <!--  -->
            <div class="select_buy plr15 space-border">
            </div>
            <!--  -->
            <div class="consultation-info space-border bgwhite plr15">
                <div class="user-box box1">
                    <h4 class="user-pub-tit">选择支付方式</h4>
                </div>
                <ul class="pay-types">
                    <li data-type="zhifubao" class="pay-type zhifubao">支付宝</li>
                    <li data-type="wechat" class="pay-type wechat active">微信</li>
                    <!-- <li data-type="bank" class="pay-type bank">在线支付</li> -->
                </ul>
            </div>
            <!--  -->
            <div class="plr15">
                <div class="submit" id="pay">确定提交</div>
            </div>
        </div>
    </div>
    <script src="https://g.alicdn.com/de/prismplayer/2.9.3/aliplayer-h5-min.js"></script>
    <script src="js/jquery-3.6.0.min.js"></script>
    <script src="js/jquery-weui.min.js"></script>
    <script src="js/md5.js"></script>
    <script src="js/public.js"></script>
    <script>
        let id

        var code = getQueryString('code');
        var openid;
        var orderNo = '';

        gethttp('/lt/goods?type=c', {
            hasToken: true,
            success: function (res) {
                let list = res.data.list
                id = list[0].id
                list.forEach((item, index) => {
                    let html = `<div class="tabs_buy_item ${index == 0 ? 'active' : ''} ${item.type == 1 ? 'hot' : ''}" data-id="${item.id}" data-days="${item.days}" data-sellingPrice="${item.sellingPrice}">
                                    <a href="javascript:;">
                                        <p class="tabs_buy_title">${item.name}</p>
                                        <p class="tabs_buy_price">￥<span>${item.sellingPrice}</span></p>
                                        <p class="tabs_buy_oldprice">￥${item.labelPrice}</p>
                                    </a>
                                </div>`
                    $('.tabs_buy').append(html)
                });
                $('.select_buy').html(`<p class="select_time">${list[0].days}天权益</p>
                                    <p class="select_price"><span>${list[0].sellingPrice}</span> 元</p>`)
            }
        })

        $(document).on('click', '.tabs_buy_item', function () {
            id = $(this).attr('data-id')
            let days = $(this).attr('data-days')
            let sellingPrice = $(this).attr('data-sellingPrice')
            $(this).addClass('active').siblings().removeClass('active')
            $('.select_buy').html(`<p class="select_time">${days}天权益</p>
                                    <p class="select_price"><span>${sellingPrice}</span> 元</p>`)
        })

        $('.pay-types li').click(function () {
            $(this).addClass('active').siblings().removeClass('active');
        })

        if (code) {
            // 获取openid
            getOpenid();
        } else {
            $('#pay').click(function () {
                var token = getCookie('token');
                var user = getCookie('userId');
                if (!token || !user) {
                    window.location.href = 'login-code.html';
                    return false;
                }

                $.showLoading();
                var type = $('.pay-types li.active').attr('data-type');
                if (type === 'wechat') {
                    // 获取code
                    getCode();
                } else if (type === 'zhifubao') {
                    getPayOption('2');
                }
            })
        }

        function getCode() {
            if (code === '' || code == null) {
                var thisUrl = window.location.href;
                // var thisUrl = 'https://nqct.ip58.lhyw.net/youmai/ot-classroom-buyCard.html'
                thisUrl = encodeURIComponent(thisUrl);
                var theWx = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + appId + '&redirect_uri=';
                var url = theWx + thisUrl + '&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect';
                window.location.href = url;
            }
        }

        function getOpenid() {
            var aJson = {
                'jsCode': code,
            };
            $.ajax({
                url: host + '/user/goVxLogin',
                method: 'POST',
                dataType: 'json',
                contentType: 'application/json',
                data: JSON.stringify(aJson),
                success: function (res) {
                    console.log(res);
                    if (res.code == '01') {
                        openid = res.data.openid;
                        getPayOption('1');
                    } else {
                        toast(res.message);
                    }
                }
            });
        }

        function getPayOption(orderType) {
            posthttp('/order', {
                hasToken: true,
                data: {
                    type: 'c',
                    goods: id,
                    user: getCookie('userId')
                },
                success: function (res) {
                    orderNo = res.data.saved.code
                    var option = {
                        orderNo: orderNo,
                        orderType: orderType,
                        openid: openid
                    }
                    http('/pay/payMoney', {
                        hasToken: false,
                        data: option,
                        success: function (res) {
                            if (res.code === '01') {
                                if (orderType == '1') {
                                    wecahtPay(orderType, res.data);
                                } else if (orderType == '2') {
                                    console.log(res.data)
                                    $(document.body).append(res.data)
                                }
                            }
                        },
                        fail: function () {
                            $.hideLoading();
                        }
                    })
                }
            })

        }

        function wecahtPay(orderType, data) {
            var o = {
                "appId": data.appId, //公众号ID，由商户传入     
                "timeStamp": data.timeStamp, //时间戳，自1970年以来的秒数     
                "nonceStr": data.nonceStr, //随机串     
                "package": data.package,
                "signType": data.signType, //微信签名方式：     
                "paySign": data.paySign //微信签名 
            }
            WeixinJSBridge.invoke(
                'getBrandWCPayRequest', o,
                function (res) {
                    $.hideLoading();
                    if (res.err_msg == "get_brand_wcpay_request:ok") {
                        // 使用以上方式判断前端返回,微信团队郑重提示：
                        //res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
                        // updateOrder(orderType, '04')
                        window.location.replace('ot-classroom-buyCardGood.html?orderNo=' + orderNo);
                    }
                    // else {
                    // updateOrder(orderType, '05')
                    // }
                }
            );
        }
    </script>
</body>

</html>